package managment.impl;

import java.util.Date;

import lombok.Data;
import managment.interfeces.IManagment;

import org.apache.log4j.Logger;

import producedepartment.impl.ProduceDepartment;
import product.enums.Category;
import product.impl.Product;
import saledepartment.impl.SaleDepartment;

/**
 * Komponent odpowiedzialny za zarzadzanie firma
 * @author przemek
 *
 */
@Data
public class Managment implements IManagment {

	static final Logger log = Logger.getLogger(Managment.class);
	private ProduceDepartment produceDepartment;
	private SaleDepartment saleDepartment;
	private static final int PRODUCT_UNIT_TIME = 5;


	@Override
	/**
	 * Metoda wyceniajaca zamowienie
	 */
	public int measuredOrder(Product p) {
		int productCurrentTime =  produceDepartment.getProductCount(p);
		return p.getCount() - productCurrentTime;
	}

	@Override
	/**
	 * Metoda zwracajacy dany 
	 */
	public Product delegateProduce(Product product) {
		log.info("Realizuje zamowienie dla produktu nazwa: " +product.getName() + "kategoria: " + product.getCount());
		if(isProductAvaible(product)){
			return getProductFromMagazin(product);
		}
		else{
			return this.produceDepartment.produce(product);
		}
		
	}
	/**
	 * Metoda pobierajaca produkt z magazynu
	 * @param product
	 * @return
	 */
	private Product getProductFromMagazin(Product product){
		return produceDepartment.getProduktFromMagazin(product);
	}
	
	public void analyze(String productName, Category category) {
		produceDepartment.produce(new Product(productName,PRODUCT_UNIT_TIME,category));
	}
	/**
	 * Metoda sprawdzajaca czy produkt jest dostepny
	 * @param p
	 * @return
	 */
	public boolean isProductAvaible(Product p) {
		return produceDepartment.isProductAvaible(p);
	}
	/**
	 * Dodaje nowego kupione produkty 
	 * potrzebne do produkcji
	 * @param count
	 */
	public void addBuyProductCount(int count) {
		produceDepartment.addBuyProductCount(count);
		
	}
	/**
	 * Szacuje date zamowienia
	 * @param product
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public Date measuredOrderDate(Product product) {
		Date currentDate = new Date();
		currentDate.setMinutes(currentDate.getMinutes() - produceDepartment.getProductCount(product));
		return currentDate;
	}

}
